package com.qingzhu.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.qingzhu.service.PayService;
import com.qingzhu.util.CreatedAlipayClient;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class PayServiceImpl implements PayService {
    private final static String OUT_TRADE_NO = "20150320010101006";

    @Override
    public Map<String,String> toPay(String orderId, String sunShopPrice) {
        // 初始化支付宝客户端
        AlipayClient alipayClient = new CreatedAlipayClient().getAlipayClient();
        // 创建API对应的request类
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        // 转换金额的格式；保证金额始终是以元为单位;否则支付宝客户端将无法正常响应
        Double fee = Double.parseDouble(sunShopPrice);
        // 设置业务参数
        request.setBizContent("{" +
                "    \"out_trade_no\":\""+OUT_TRADE_NO+"\"," +
                "    \"total_amount\":\""+fee+"\"," +
                "    \"subject\":\"Iphone6 16G\"," +
                "    \"store_id\":\"NJ_001\"," +
                "    \"timeout_express\":\"90m\"}");
        AlipayTradePrecreateResponse response = null;
        try {
            // 通过客户端发送请求
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        // 获取响应数据
        Map<String,String> map = new HashMap<>();
        if (response != null) {
            String code = response.getCode();
            // 如果客户端正常响应就获取响应的数据
            if ("10000".equals(code)) {
                map.put("payUrl",response.getQrCode());
                map.put("orderId",orderId);
                map.put("sunShopPrice",String.valueOf(fee));
                map.put("code",code);
                System.out.println("预付款信息："+map);
            } else {
                map.put("msg","每周日中午12点至每周一中午12点将对沙箱环境进行维护,期间可能出现不可用，敬请谅解！沙箱响应："+code);
                map.put("sub_msg",response.getSubMsg());
                map.put("code",code);
                System.out.println("预付款失败！每周日中午12点至每周一中午12点将对沙箱环境进行维护,期间可能出现不可用，敬请谅解。沙箱响应："+code);
            }
        } else {
            map.put("msg","请求支付宝沙箱客户端时出现问题！");
            map.put("code","500");
            System.out.println("请求支付宝沙箱客户端时出现问题！");
        }
        return map;
    }

    @Override
    public Map<String, String> queryPayStatus(String orderId) {
        // 初始化支付宝客户端
        AlipayClient alipayClient = new CreatedAlipayClient().getAlipayClient();
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        // 设置业务参数
        request.setBizContent("{" +
                "    \"out_trade_no\":\""+OUT_TRADE_NO+"\"," +
                "    \"trade_no\":\"\"}");
        AlipayTradeQueryResponse response = null;
        try {
            // 通过客户端发送请求
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        // 获取响应数据
        Map<String,String> map = new HashMap<>();
        if (response != null) {
            String code = response.getCode();
            // 如果客户端正常响应就获取响应的数据
            if ("10000".equals(code)) {
                map.put("orderId",orderId);
                map.put("tradeStatus",response.getTradeStatus());
                System.out.println("预付款信息："+map);
            } else {
                map.put("msg","每周日中午12点至每周一中午12点将对沙箱环境进行维护,期间可能出现不可用，敬请谅解！沙箱响应："+code);
                System.out.println("预付款失败！每周日中午12点至每周一中午12点将对沙箱环境进行维护,期间可能出现不可用，敬请谅解。沙箱响应："+code);
            }
        } else {
            map.put("msg","服务异常！请求支付宝沙箱客户端时出现问题！");
            System.out.println("请求支付宝沙箱客户端时出现问题！");
        }
        return map;
    }
}
